package com.neuedu.array;

/**
 * 二分查找    二分查找的前提是需要查找的数组必须是已排序的
 * <p>
 * <p>
 * int[] sort = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 */
public class BinarySearch {

    public static void main(String[] args) {
        int findNum = 18;
        int[] sort = {2, 7, 18, 22, 32, 35};

        int low = 0;
        int high = sort.length - 1;
        int mid = high / 2;
        int findIndex = -1;

        // 判断mid所在的数字  和  查找的元素
        while (low < high) {
            if(findNum > sort[mid]){
                low = mid;
            }else if(findNum < sort[mid]){
                high = mid;
            }else{
                findIndex = mid;
                System.out.println("找到了  mid ");
                break;
            }
            mid = high / 2;
        }

        System.out.println("findIndex: "+findIndex);

    }
}
